from gltf import *
from vtkmodules.vtkRenderingCore import (
    vtkRenderWindow,
    vtkRenderWindowInteractor,
)

from visualizationCom.splitRender_interactor import mouseInteractorEvent
from visualizationCom.tools.color import hex_to_rgba

import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))


class Window:
    def __init__(self, parent, state, ctrl) -> None:
        self.webState = state
        self.webCtrl = ctrl
        self.parent = parent

        # 定义相机初始化位置，用于重置
        self.initial_position = 0
        self.initial_focal_point = 0
        self.initial_view_up = 0

        self.engine = GLTFRenderEngine()
        self.camera = self.engine.GetCamera()

        self.renderer = self.engine.GetDefaultRenderer()
        # bg_color = hex_to_rgba('#132c4a')
        bg_color = hex_to_rgba('#132c4a')
        self.renderer.SetBackground(bg_color[0], bg_color[1], bg_color[2])

        # render window
        self.renWin = vtkRenderWindow()
        # self.renWin.OffScreenRenderingOn()
        self.engine.SetRenderWindow(self.renWin, layers=False)

        # self.iren = vtkRenderWindowInteractor()
        # self.iren.SetRenderWindow(self.renWin)
        # self.iren.Initialize()

        # style = InteractorStyle()
        # style.SetEngine(self.engine)
        # style.OnPickSceneNode = self._OnPickSceneNode
        # self.iren.SetInteractorStyle(style)

        # 创建一个vtkRenderWindowInteractor对象
        self.interactor = vtkRenderWindowInteractor()
        self.interactor.SetRenderWindow(self.renWin)

        # 设置交互器样式,这个是之前自己写的，现在用石老师的，避免每次都要修改代码
        style = mouseInteractorEvent(
            self, self.webState, self.webCtrl)
        style.SetDefaultRenderer(self.renderer)
        style.SetOutlineRenderer(self.engine.GetOutlineRenderer())
        self.interactor.SetInteractorStyle(style)

        self.mouseInteractorEvent = style  # 前端需要触发里面的事件

        self.interactor.Initialize()

        #是否添加右上角的方向坐标
        # self.engine.SetInteractor(self.iren)    
        #不显示左下角的FPS
        self.renderer.showFPS = False

        self.view = None
        self.OnPickSceneNode = None

    def LoadModel(self, modelPath, clear=False):
        loader = GLTFLoader()

        CURRENT_DIRECTORY = os.path.dirname(os.path.dirname(__file__))

        # 结合 CURRENT_DIRECTORY 和 user_input 形成新的地址
        new_directory = os.path.join(CURRENT_DIRECTORY, modelPath)

        loader.Load(new_directory)
        camera = self.camera
        self.initial_position = camera.GetPosition()
        self.initial_focal_point = camera.GetFocalPoint()
        self.initial_view_up = camera.GetViewUp()

        # # set actor and reset camera...
        scene = loader.GetScene()
        self.SetScene(scene, clear)

        if len(loader.animations):
            self.parent.animationFlag = True
        else:
            self.parent.animationFlag = False

        return scene

    def SetScene(self, scene, clear=False):
        if clear:
            self.renderer.ClearScene()

        self.renderer.AddScene(scene)
        self.restCamera()
        self.renderer.ResetCameraClippingRange()

    def restCamera(self):
        # 获取当前相机
        camera = self.renderer.GetActiveCamera()

        # 重置相机的位置和方向为初始状态
        # camera.SetPosition(self.initial_position)
        # camera.SetFocalPoint(self.initial_focal_point)
        # camera.SetViewUp(self.initial_view_up)
        # self.renderer.ResetCamera()

        self.camera.Dolly(0.25)
        camera.SetPosition(0.08186602312058017, 2.5500787242916068, -3.0271407084926043)
        camera.SetFocalPoint(-0.07017824476545374, 0.1535971978574859, 0.17188396069376796)
        camera.SetViewUp(0.004540031416274459, 0.8002227536205254, 0.5996856949292048)

    def LoadEnvmap(self, envmap):
        self.renderer.SetEnvmap(envmap)

    def GetVtkWindow(self) -> vtkRenderWindow:
        return self.renWin

    def UpdateView(self):
        if self.view:
            self.view.update()

    def _OnPickSceneNode(self, scene, node):
        if self.OnPickSceneNode:
            self.OnPickSceneNode(scene, node)

    def changeEvn(self,bgIndex):
        if bgIndex == 1:
            bg_color = hex_to_rgba('#041A21')
            self.renderer.SetBackground(bg_color[0], bg_color[1], bg_color[2])
        else:
            bg_color = hex_to_rgba('#132c4a')
            self.renderer.SetBackground(bg_color[0], bg_color[1], bg_color[2])
        self.parent.renderWindow.Render()
        self.webCtrl.view_update()
        self.webCtrl.view_update()
